package cn.cpc.org.service.impl;

import cn.cpc.basic.util.PageList;
import cn.cpc.org.domain.Department;
import cn.cpc.org.mapper.DepartmentMapper;
import cn.cpc.org.query.DepartmentQuery;
import cn.cpc.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional(readOnly = true)
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    public void save(Department department) {
        departmentMapper.save(department);

    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    public PageList<Department> queryData(DepartmentQuery query) {
        Long count = departmentMapper.queryCount(query);

        if(count == 0){
            return new PageList<>();
        }

        List<Department> departments = departmentMapper.queryData(query);

        return new PageList<>(count,departments);
    }
}
